package com.cn.webspider;

import org.junit.Test;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class DownMovie implements PageProcessor {
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    public void process(Page page) {
        if (page.getUrl().get().contains("video")) {
            String name = page.getHtml().xpath("//div[@id=ajax-container]/div[@class=model-hide]/div[@class=player]/div[@class=a1]/text()").get();
            String movieUrl = page.getHtml().xpath("//div[@id=ajax-container]/div[@class=model-hide]/div[@class=player]/div[@class=player-background]/div[@class=video-js-box]//a/@href").get();
            saveMovie(name, movieUrl);
        } else {
            Selectable xpath = page.getHtml().xpath("//div[@id=container]/div[@class=videos]/div[@class=video]/p/a").links();
            page.addTargetRequests(xpath.all());
//            //下一页
            List<String> pages = page.getHtml().xpath("//div[@class=page]/a").links().all();
            page.addTargetRequest(pages.get(pages.size() - 1));
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Spider.create(new DownMovie()).addUrl("http://41amm.com/diao/se57.html")
                .thread(5)
//                .setScheduler(new FileCacheQueueScheduler("E:\\spidermovie"))
                .run();

//        Spider.create(new DownMovie()).addUrl("http://41amm.com/diao/se55.html").thread(5).setScheduler(new FileCacheQueueScheduler("E:\\spidermovie")).run();
//        Spider.create(new DownMovie()).addUrl("http://www.baidu.com/").run();
    }

    int count = 1;

    public void saveMovie(String movieName, String movieUrl) {
        String dirc = "E:\\spidermovie\\guochan";
        URL url = null;
        DataInputStream dataInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            File file = new File(dirc);
            if (!file.exists()) {//如果文件夹不存在
                file.mkdir();//创建文件夹
            }
            url = new URL(movieUrl);
            dataInputStream = new DataInputStream(url.openStream());
            fileOutputStream = new FileOutputStream(new File(dirc + "\\" + movieName + movieUrl.substring(movieUrl.lastIndexOf("."))));
            byte[] bytes = new byte[1024 * 1024];
            int length;
            while ((length = dataInputStream.read(bytes)) > 0) {
                fileOutputStream.write(bytes, 0, length);
            }
            System.out.println("已下载" + count++ + "部");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                dataInputStream.close();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    @Test
    public void save() {
        saveMovie("ceshi", "https://201606mp4.11bubu.com/20161107/tem-036/1/xml/91_5a8cbd003f8d4304df7b3974a6d61219.mp4");
    }
}
